﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Gav.Data;
using Gav.Graphics;
using Microsoft.DirectX;

using Gav.Components.MapLayers;
using Gav.Components;
using Gav.Components.GavLayers;
using Gav.Management;
using Gav.Components.Internal;

namespace Infovizprojekt
{
    public partial class MainWindow : Form
    {
        
        Panel _mapPanel;
        Panel _pcPanelVariables;
        Panel _pcPanelYears;
        Panel _scatterPanel;
        Panel _tableLensPanel;
        Panel _colorLegendPanel;

        public MapHandler MapHandler { get; set; }
        public DataHandler DataHandler { get; set; }
        public PCHandler PCHandler { get; set; }
        public PlotHandler PlotHandler { get; set; }
        public ColorLegendHandler ColorLegendHandler { get; set; }

        public ViewManager ViewManager { get; set; }
        public ColorMap mainColorMap { get; set; }

        public bool usingKMeans = false;

        public SelectionAndPickingManager sapManager = new SelectionAndPickingManager();

        public const int PC_VAR_PLOT_INIT_VALUE = 0;
        public const int PC_YEARS_PLOT_INIT_VALUE = 0;
        public const int SCATTER_PLOT_Y_INIT_VALUE = 0;
        public const int SCATTER_PLOT_X_INIT_VALUE = 1;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _mapPanel = splitContainer5.Panel2;
            _pcPanelVariables = pcVariablePanel;
            _pcPanelYears = pcYearPanel;
            _scatterPanel = tabPage1;
            _tableLensPanel = tabPage2;
            _colorLegendPanel = splitContainer6.Panel2;

            DataHandler = new DataHandler(this);
            ViewManager = new ViewManager(this);

            //Manage the color map
            mainColorMap = new ColorMap();
            mainColorMap.AddColorMapPart(new LinearHsvColorMapPart(100f, 200f));
            mainColorMap.NaNColor = Color.LightGray;
            
            mainColorMap.Index = 0;
            mainColorMap.Input = DataHandler.MainDataCubeProvider;


            MapHandler = new MapHandler(this);
            PCHandler = new PCHandler(this);
            PlotHandler = new PlotHandler(this);
            ColorLegendHandler = new ColorLegendHandler(this);

            sapManager.Picked += new EventHandler<Gav.Event.IndexesPickedEventArgs>(sapManager_Picked);
        }

        void sapManager_Picked(object sender, Gav.Event.IndexesPickedEventArgs e)
        {
            this.MapHandler._mainChoroplethMap.Invalidate();
            this.PCHandler._pcPlotVariables.Invalidate();
            this.PCHandler._pcPlotYears.Invalidate();
        }


        public Panel getMapPanel()
        {
            return _mapPanel;
        }

        public Panel getPCPanelVariables()
        {
            return _pcPanelVariables;
        }

        public Panel getPCPanelYears()
        {
            return _pcPanelYears;
        }

        public Panel getScatterPanel()
        {
            return _scatterPanel;
        }

        public Panel getTableLensPanel()
        {
            return _tableLensPanel;
        }

        public Panel getColorLegendPanel()
        {
            return _colorLegendPanel;
        }

        private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void resetMapButton_Click(object sender, EventArgs e)
        {
            MapHandler.resetMapScaleAndPosition();
        }

        private void yesToKmeans_Click(object sender, EventArgs e)
        {
            PCHandler._pcPlotVariables.Input = DataHandler.KMeansDataCubeProvider.centroidCube;
            usingKMeans = true;
            ViewManager.InvalidateAll();
        }

        private void noToKmeans_Click(object sender, EventArgs e)
        {
            PCHandler._pcPlotVariables.Input = DataHandler.MainDataCubeProvider;
            usingKMeans = false;
            ViewManager.InvalidateAll();
        }

        private void playTimeButton_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < (DataHandler.EndYear-DataHandler.StartYear); i++)
            {

                yearSlider.Value = i;
                ViewManager.InvalidateAll();
                System.Threading.Thread.Sleep(250);
            }
        }

        private void numberofClusters_ValueChanged(object sender, EventArgs e)
        {
            this.DataHandler.KMeansDataCubeProvider.K = (int)numberofClusters.Value;
            this.DataHandler.KMeansDataCubeProvider.CommitChanges();
            ViewManager.InvalidateAll();
        }

    }
}